package controller;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.LinkedList;
import java.util.Stack;

import model.PlayingField;

public class Mainfunction {
	
	
	
	

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		PlayingField pf = new PlayingField();
		PlayingField pf2 = new PlayingField();
		ArtificialSolver as = new ArtificialSolver();
		
		pf.setToFireplaceStart();
		pf2.setToFireplaceStart();
		as.printGameState(pf);
		DFSObject initialObj = new DFSObject(pf, 11, new LinkedList<MoveForward>());//normal:44, X:9, fire:11 
		Stack<DFSObject> st = new Stack<DFSObject>();//SmallerCross:32, pyra=16, arrow=17, Diamond=24
		st.push(initialObj);
		LinkedList<DFSObject> solutions  = new LinkedList<DFSObject>();
		as.doDFS(st, solutions);
		
		try {
			LinkedList<DFSObject> solutions2 = as.loadSolutions("fireplace", pf2);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
//		pf.setToFireplaceStart();
//		as.printAllPossibleMovesForward(pf);
//		pf=as.moveForward(pf, 3, 4, 1, 4);
//		as.printAllPossibleMovesForward(pf);
//		pf=as.moveForward(pf, 5, 4, 3, 4);
//		as.printGameState(pf);
//		pf=as.moveForward(pf, 4, 3, 4, 1);
//		as.printGameState(pf);
//		int[][] re = pf.getThreeStatesPlayingField();
		
//		for(int y=0;y<re[0].length;y++){
//			for(int x=0;x<re.length;x++){
//				System.out.print(re[x][y]+" ");
//			}
//			System.out.println();
//		}
		
		
//		pf.setToSmallerCrossStart();
//		as.printGameState(pf);
//		
//		
		for(int i=0;i<1;i++){
			
			MoveForward mf = as.findAllPossibleMovesForward(pf).get(1);
			pf = as.moveForward(pf, mf.getxStart(), mf.getyStart(), mf.getxEnd(), mf.getyEnd());
			
			MoveForward mf1 = pf.getLastMoveForward();
			System.out.println(" MoveForward: ["+mf1.getxStart()+","+mf1.getyStart()+"] ["+mf1.getxEnd()+","
			+mf1.getyEnd()+"]");
		}
		
		for(int i=0;i<1;i++){
			
			MoveForward mf = as.findAllPossibleMovesForward(pf).get(9);
			pf = as.moveForward(pf, mf.getxStart(), mf.getyStart(), mf.getxEnd(), mf.getyEnd());
			
			MoveForward mf1 = pf.getLastMoveForward();
			System.out.println(" MoveForward: ["+mf1.getxStart()+","+mf1.getyStart()+"] ["+mf1.getxEnd()+","
			+mf1.getyEnd()+"]");
		}
//		
		as.printGameState(pf);
		System.out.println("Solutions: "+as.amountOfSolutionsLeft(solutions, pf));
		System.out.println("Solutions2: "+as.amountOfSolutionsLeft(solutions, pf2));
//		
//		MoveForward mf = as.findAllPossibleMovesForward(pf).get(0);
//		pf = as.moveForward(pf, mf.getxStart(), mf.getyStart(), mf.getxEnd(), mf.getyEnd());
//		
//		MoveForward mf1 = pf.getLastMoveForward();
//		System.out.println(" MoveForward: ["+mf1.getxStart()+","+mf1.getyStart()+"] ["+mf1.getxEnd()+","
//		+mf1.getyEnd()+"]");
//		
//		as.printGameState(pf);
//		System.out.println("Valid: "+as.isMoveBackwardValid(pf,4,4,2,4));
//		pf = as.moveBackward(pf, mf.getxEnd(),mf.getyEnd(),mf.getxStart(),mf.getyStart());
////		System.out.println("Valid2: "+pf.getStackForward().size());
//		
//		
//		
//		MoveBackward mf2 = pf.getLastMoveBackward();
//		System.out.println(" MoveBackward: ["+mf2.getxStart()+","+mf2.getyStart()+"] ["+mf2.getxEnd()+","
//		+mf2.getyEnd()+"]");
//		
//		pf = as.moveForward(pf,2,4,4,4);
////		System.out.println("Valid3: "+pf.getStackBackward().size());
//		
//		System.out.println("Valid: "+as.isMoveBackwardValid(pf,4,4,2,4));
//		pf = as.moveBackward(pf, 4,4,2,4);
		
		
		
		
		
		
//		int [][] d = as.buildAIJ(pf);
//		for(int i=0;i<d[0].length;i++){
//			for(int j=0;j<d.length;j++){
//				System.out.print(" "+ d[j][i]);
//			}
//			System.out.println("");
//		}
//		
//		int[] c = as.buildPse(pf); 
//		for(int i=0;i<c.length;i++){
////			if(i!=17){
////				c[i]=0;
////			}
////			else{
////				c[i]=1;
////			}
//			System.out.println(""+ c[i]);
//		}
//		
//		 int N = 33;
//	        double[][] A = { { 0, 1,  1 },
//	                         { 2, 4, -2 },
//	                         { 0, 3, 15 }
//	                       };
//	        double[] b = { 1, 1, 8 };
//	        double[] x = as.lsolve(d, c);


	        // print results
//	        for (int i = 0; i < N; i++) {
//	            System.out.println(x[i]);
//	        }


	}

}
